package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.zhentao.common.dtos.ResponseResult;
import com.zhentao.common.enums.AppHttpCodeEnum;
import com.zhentao.constants.OperateConstant;
import com.zhentao.constants.UserThreadLocal;
import com.zhentao.dating.dto.LikeDto;
import com.zhentao.service.LikeDatingService;
import com.zhentao.user.domain.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class LikeDatingServiceImpl implements LikeDatingService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public ResponseResult likeOperate(LikeDto dto) {
        if(dto.getId() == null || dto.getOperation() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AppUser userInfo = UserThreadLocal.getUserInfo();
        if(userInfo == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if(dto.getOperation() == 1){
            Object o = stringRedisTemplate.opsForHash().get(OperateConstant.LIKE_KEY + dto.getId(), userInfo.getId());
            if(o != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"已点赞");
            }
            stringRedisTemplate.opsForHash().put(OperateConstant.LIKE_KEY + dto.getId(),userInfo.getId(), JSON.toJSONString(dto));

        }else {
            Object o = stringRedisTemplate.opsForHash().get(OperateConstant.LIKE_KEY + dto.getId(), userInfo.getId());
            if(o == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"未点赞");
            }
            stringRedisTemplate.opsForHash().delete(OperateConstant.LIKE_KEY + dto.getId(),userInfo.getId());

        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
